package com.tadpole.music.view.widget;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import com.tadpole.music.R;
import com.tadpole.music.bean.PushBean;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.ToastUtils;
import com.tadpole.music.view.activity.web.H5Activity;
import razerdp.basepopup.BasePopupWindow;

/**
 * Created By 孙岩
 * on 2021/6/22
 */
public class ZhengCePopup extends BasePopupWindow implements View.OnClickListener {
    private Activity activity;
    private TextView tvText;
    private TextView tvNo;
    private TextView tvYes;
    private IModeSelection iModeSelection;

    public ZhengCePopup(Activity activity) {
        super(activity);
        this.activity = activity;
        tvText = (TextView) findViewById(R.id.tvText);
        tvNo = (TextView) findViewById(R.id.tvNo);
        tvYes = (TextView) findViewById(R.id.tvYes);
        tvNo.setOnClickListener(this);
        tvYes.setOnClickListener(this);
        initData();
        getPopupWindow().setOutsideTouchable(false);
        getPopupWindow().setFocusable(false);
        getPopupWindow().setTouchable(true);
    }

    @Override
    public boolean onDispatchKeyEvent(KeyEvent event) {
        if(getPopupWindow()!=null&&getPopupWindow().isShowing()){
            return false;
        }else{
            return super.onDispatchKeyEvent(event);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    @Override
    public boolean onBackPressed() {
        if(getPopupWindow()!=null&&getPopupWindow().isShowing()){
            return  false;
        }else{
            return super.onBackPressed();
        }
    }


    private void initData(){
        SpannableString spannable = new SpannableString ("欢迎您使用蝌蚪音基！我们非常重视您的隐私保护和个人信息保护，特别提示您阅读并充分理解\"隐私政策\"各条款。\n" +
                "我们会严格按照法律规定存储和使用您的个人信息，未经您的同意，我们不会提供给任何第三方进行使用。我们会采取业界领先的安全措施保护您的个人信息安全。\n" +
                "您可以阅读《隐私政策》全文了解详细信息。如您同意，请点击“同意”开始接受我们的服务");
        //这个一定要记得设置，不然点击不生效
        tvText.setMovementMethod(LinkMovementMethod.getInstance());
        tvText.setHighlightColor(Color.TRANSPARENT);
        //隐私政策
        spannable.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                if(Mutils.isNetworkAvailable()){
                    PushBean pushBean = new PushBean();
                    pushBean.setUrl("https://kedou.aiyuechen.com/api/v2/notice/81");
                    pushBean.setTitle("隐私政策");
                    Intent intent = new Intent(activity, H5Activity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("params",pushBean);
                    intent.putExtras(bundle);
                    activity.startActivity(intent);
                }else{
                    ToastUtils.show("请检查您的网络设置");
                }
            }
            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setLinearText(false);
            }
        }, 131, 137, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        //设置文字的前景色

        spannable.setSpan(getRedSpan(),131,137, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvText.setText(spannable);
    }

    private ForegroundColorSpan getRedSpan(){
        return new ForegroundColorSpan(ContextCompat.getColor(activity, R.color.colorMain));
    }

    @Override
    protected Animation initShowAnimation() {
        return getDefaultAlphaAnimation();
    }

    @Override
    public View getClickToDismissView() {
        return getPopupWindowView();
    }

    @Override
    public View onCreatePopupView() {
        return createPopupById(R.layout.pop_zhengce);
    }

    @Override
    public View initAnimaView() {
        return findViewById(R.id.rl_auth);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvNo:
                iModeSelection.getMode(1);
                break;
            case R.id.tvYes:
                iModeSelection.getMode(2);
                break;
        }
    }

    public void setModeListener(IModeSelection iModeSelection) {
        this.iModeSelection = iModeSelection;
    }

    public interface IModeSelection{
        void getMode(int mode);
    }
}
